<?php
namespace app\demo\controller;
//导入模型类
use app\demo\model\Staff;
use app\demo\model\User;
use think\Db;
use think\paginator\driver\Bootstrap;

class Index
{
    public function index()
    {


        //①数据对象
        $data['id']  = 10;
        $data['name'] = 'liming';
        $data['version'] = 1.10;

        //创建模型类Staff
        $model = new Staff($data);
        //dump($model);

        //②数据对象
        $model1 = new Staff();
        $model1 -> data('id','10');//$data['id'] = 10
        $model1 -> data('name','Peter');  //$data['name'] = 'Peter'
        $model1 -> data('age',30);      //$data['age'] = 3
        $model1->data("version",'1211');

        //dump($model1);

        //③数据对象
        $model2 = new Staff();

//        $model2->setAttr('id','11');
        $model2 -> setAttr('id',10);
        $model2 -> setAttr('name','Jack');
        $model2 -> setAttr('age',20);
        //dump($model2);

        //④_set

        $model3 = new Staff();
        $model3->id = '32';
        $model3->age = 23;
        $model3->adress = "afaff ";

        //dump($model3);

        //5.获取数据对象原始数据:getData()
        $model4 = new Staff();

        $data = [];

        $data['id'] = '11';
        $data['name'] = "joker";

        $model4->data = $data;
//        $result = $model4->getData();
        $result = $model2->getData("name");
//        $result = $geteData['name'];
        dump($result);

     /*   $model5 = new Staff();
        $data1 = [];
        $data1['id'] = 1001;
        $data1['name'] = 'Peter';
        $data1['age'] = 26;
        //3.data()方法完成前面模型对象的$data属性赋值  //使模型对象转换成数据对象,现在$model 已经是数据对象啦
         $model -> data($data1);  //好像这种复赋值  对象$data赋值 测试了有问题
        $result1 = $model5 -> getData('name'); ??? why 错误
        dump($result1);*/

        $a = $model2->getAttr('name');
        dump($a);


        //$modelfied = $model->field;
        //查看Staff类实例$model


        // 获取`think_user`表所有信息
//        $a = Db::getTableInfo('tp5_staff');
   /*     // 获取`think_user`表所有字段
        $b = Staff::getTableInfo('tp5_staff', 'fields');
        // 获取`think_user`表所有字段的类型
        $c = Staff::getTableInfo('tp5_staff', 'type');
        // 获取`think_user`表的主键
        $d = Staff::getTableInfo('tp5_staff', 'pk');*/

    }

    public function getIndex(){
        //1.获取表中单条记录
        $result = Staff::get(1005) //静态调用get方法,主键查询
        -> getData();    //获取数据对象中原始数据


        $result = Staff::get(1001); //静态调用get方法,主键查询

        //2.查询原始数据
        dump($result);
    }

    public function getData(){

//        取出主键为1的数据
        $user = Staff::get(1);
        echo $user->name;

        // 使用数组查询
        $user = Staff::get(['name' => '潘金莲']);

        // 使用闭包查询
        $user = Staff::get(function($query){
            $query->where('name', '宋江');
        });
        echo $user->name;
    }

}
